package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ZqsyDao;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：债权收益界面
 */

@Repository
public class ZqsyDaoImpl  extends P2PJdbcDao implements ZqsyDao{

	/**
	 * @author zhai
	 * 获取债券收益的数据集
	 */
	@Override
	public List queryZqsyList(JSONObject params) {

		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		
		String tzryhm=params.optString("tzryhm");
		String tzrsjhm=params.optString("tzrsjhm");
		String syryhm=params.optString("syryhm");
		String syrsjhm=params.optString("syrsjhm");
		String state=params.optString("state");
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("TZRYHM", tzryhm);
		paramMap.put("TZRSJHM", tzrsjhm);
		paramMap.put("SYRYHM", syryhm);
		paramMap.put("SYRSJHM", syrsjhm);
		paramMap.put("state", state);
		
		
		
		sql.append(" SELECT *,date_format(SYFF_DATE,'%Y-%m-%d %h:%i:%s') as SYFF_DATE FROM v_p2p_zqff_syff_info ");
		sql.append("  where 1=1 ");
		
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND SYFF_DATE >= :startDate");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND SYFF_DATE <= :endDate");
			paramMap.put("endDate",endDate);
		}
		
		if(StringUtils.isNotEmpty(tzryhm)){
			sql.append(" AND TZR_USER_ID like "+ "'%"+tzryhm+"%'");
		}
		if(StringUtils.isNotEmpty(tzrsjhm)){
			sql.append(" AND TZR_ZC_SJHM like "+ "'%"+tzrsjhm+"%'");
		}
		if(StringUtils.isNotEmpty(syryhm)){
			sql.append(" AND SYR_USER_ID like "+ "'%"+syryhm+"%'");
		}
		if(StringUtils.isNotEmpty(syrsjhm)){
			sql.append(" AND SYR_ZC_SJHM like "+ "'%"+syrsjhm+"%'");
		}
		
		
		if((StringUtils.isNotEmpty(state))&&(state.equals("00"))){
			System.out.println("查询全部记录。");
		}
		if((StringUtils.isNotEmpty(state))&&(state.equals("01"))){
			sql.append(" AND SYFF_STATE = 01 ");
		}
		if((StringUtils.isNotEmpty(state))&&(state.equals("02"))){
			sql.append(" AND SYFF_STATE = 02 ");
		}
		sql.append(" order by SYFF_DATE desc LIMIT :START,:LIMIT");
		return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);

	}

	/**
	 * @author zhai
	 * 获取债权收益的数据个数
	 */
	@SuppressWarnings("deprecation")
	@Override
	public int getZqsyCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");


		String tzryhm=params.optString("tzryhm");
		String tzrsjhm=params.optString("tzrsjhm");
		String syryhm=params.optString("syryhm");
		String syrsjhm=params.optString("syrsjhm");
		String state=params.optString("state");
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("TZRYHM", tzryhm);
		paramMap.put("TZRSJHM", tzrsjhm);
		paramMap.put("SYRYHM", syryhm);
		paramMap.put("SYRSJHM", syrsjhm);
		paramMap.put("state", state);
		
		sql.append(" SELECT COUNT(*) CT ");
		sql.append(" FROM v_p2p_zqff_syff_info where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND SYFF_DATE >= :startDate");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND SYFF_DATE <= :endDate");
			paramMap.put("endDate",endDate);
		}
		
		if(StringUtils.isNotEmpty(tzryhm)){
			sql.append(" AND TZR_USER_ID like "+ "'%"+tzryhm+"%'");
		}
		if(StringUtils.isNotEmpty(tzrsjhm)){
			sql.append(" AND TZR_ZC_SJHM like "+ "'%"+tzrsjhm+"%'");
		}
		if(StringUtils.isNotEmpty(syryhm)){
			sql.append(" AND SYR_USER_ID like "+ "'%"+syryhm+"%'");
		}
		if(StringUtils.isNotEmpty(syrsjhm)){
			sql.append(" AND SYR_ZC_SJHM like "+ "'%"+syrsjhm+"%'");
		}
		if((StringUtils.isNotEmpty(state))&&(state.equals("00"))){
			System.out.println("查询全部记录。");
		}
		if((StringUtils.isNotEmpty(state))&&(state.equals("01"))){
			sql.append(" AND SYFF_STATE = 01 ");
		}
		if((StringUtils.isNotEmpty(state))&&(state.equals("02"))){
			sql.append(" AND SYFF_STATE = 02 ");
		}
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

}
